<?php
	require '../../slimloader/Slim/Slim.php';
	require '../helpers/GeneralUtil.php';
	require '../helpers/Config.php';
	require '../helpers/DbHelper.php';

	\Slim\Slim::registerAutoloader();
	
	$app = new \Slim\Slim(array(
		'debug'=>true
	));
	$app->get('/jobs',function() use ($config,$app){
		$respData=array();
		$apikey = $app->request->get("devapipassword");
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		$dbHelper =new DbHelper();
		$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);
		if (mysqli_connect_errno()){
				$respData=generateError('0',"Failed to connect to MySQL: " . mysqli_connect_error());
		}else{
			$zipcode = $app->request->get("zipcode");
			if(strlen($zipcode)==5){
				if(is_numeric($zipcode)){
					$mileApart =$app->request->get("mile");
					$jobCategory = $app->request->get("jobCategory");//optional
					$jobType= $app->request->get("jobType");//optional
					$language = $app->request->get("language");//optional
					$offset = $app->request->get("offset");
					$mileApart = $mileApart!=null && $mileApart!=""?$mileApart:"5";
					$offset = $offset!=null && $offset!=""?$offset:"0";
					$attributes = array();
					if($jobType!=null && $jobType!=""){
						$attributes['jobs.jobs_types_id']=$jobType;
					}
					if($language!=null && $language!=""){
						$attributes['jobs.languages_id']=$language;
					}
					if($jobCategory!=null && $jobCategory!=""){
						$attributes['jobs.job_categories_id']=$jobCategory;
					}
					
					$latLng=$dbHelper->getLatLongByZipcode($zipcode,$con);
					if(count($latLng)>0){
						$lat = $latLng['lat'];
						$lng=$latLng['lng'];
						
						$result = $dbHelper->getLocationIdByLatLng($con,$lat,$lng,$mileApart,$offset);
						$respData=buildOutJobResults($result,$dbHelper,$con,$offset,$attributes);
					}else{
						$respData=generateError("0","There are no result for this zipcode");
					}
				}else{
					$respData=generateError("0","Zipcode must be numeric value");
				}
			}else{
				$respData=generateError("0","Zipcode must be 5 digit only");
			}	
		}
		echo json_encode($respData);
	});
	$app->get('/jobCategories',function() use($config,$app){
		$respData=array();
		$apikey = $app->request->get("devapipassword");
		if($apikey!=$config['devapipassword']){
			echo json_encode(generateError('0','Miss Match API Key'));
			exit;
		}
		$dbHelper =new DbHelper();
		$con = mysqli_connect($config['database']['host'],$config['database']['username'],$config['database']['password'],$config['database']['table']);
		$results = $dbHelper->getJobCategories($con);
		if($results){
			while($row=mysqli_fetch_array($results)){
				$catId = $row['job_categories_id'];
				$catName = $row['job_categories_name'];
				array_push($respData,array('categoryId'=>$catId,'categoryName'=>$catName));
			}
		}
		echo json_encode($respData);
	});
	$app->run();
	
?>